package com.jwgf.smarowindingmachine_unity.ui;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jwgf.Half485.fxthreadpool.FxPLCData;
import com.jwgf.Half485.modbusthreadpool.PLCData;
import com.jwgf.common.AppContext;
import com.jwgf.common.MD5;
import com.jwgf.common.ViewCache;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.qrcode.QRCodeUtil;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;
import com.jwgf.smarowindingmachine_unity.logic.SmaroWindingMachineCanReadSucceedListener;

import java.util.Locale;

public class EquipmentMaintenanceSoftwareUpgradeFragment extends BaseFragment
        implements OnTouchListener, OnClickListener {
    public static final String fragmentTag = "EquipmentMaintenanceSoftwareUpgradeFragment";

    Button exitBtn;

    private TextView APKversion;

    private TextView error_msg;

    private ImageView qrcode;


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        exitBtn = (Button) getActivity().findViewById(
                R.id.exit_btn);
        exitBtn.setOnClickListener(this);
        APKversion = (TextView) getActivity().findViewById(R.id.APK_version);
        APKversion.setText(AppContext.version);//Version
        StringBuilder builder = new StringBuilder();
        if (MachineParameterSharedPreference.getInstance().getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
            String language = Locale.getDefault().getLanguage();
            if (language.equals("en")) {
                builder.append("Main PLC Version:\t\t\t");
                builder.append("L");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0xF9));
                builder.append("J.");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0xFA));
                builder.append("P.V");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0xFB) >> 8);
                builder.append(".");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0xFB) & 0xff);
                builder.append("\r\n");

                builder.append("CBF PLC Version:\t\t\t\t");
                builder.append("L");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x131));
                builder.append("C.");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x132));
                builder.append("P.V");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x13E) >> 8);
                builder.append(".");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x13E) & 0xff);
                builder.append("\r\n");

                builder.append("Magazine PLC Version:\t\t\t");
                builder.append("L");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x15E));
                builder.append("S.");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x15F));
                builder.append("P.V");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x162) >> 8);
                builder.append(".");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x162) & 0xff);
                builder.append("\r\n");

                builder.append("AD PLC Version:\t\t\t");
                builder.append(SmaroWindingMachineCanReadSucceedListener.getInstance().dofferVersion);
            } else {
                builder.append("机头 PLC Version:\t\t\t");
                builder.append("L");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0xF9));
                builder.append("J.");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0xFA));
                builder.append("P.V");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0xFB) >> 8);
                builder.append(".");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0xFB) & 0xff);
                builder.append("\r\n");

                builder.append("CBF PLC Version:\t\t\t\t");
                builder.append("L");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x131));
                builder.append("C.");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x132));
                builder.append("P.V");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x13E) >> 8);
                builder.append(".");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x13E) & 0xff);
                builder.append("\r\n");

                builder.append("纱库 PLC Version:\t\t\t");
                builder.append("L");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x15E));
                builder.append("S.");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x15F));
                builder.append("P.V");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x162) >> 8);
                builder.append(".");
                builder.append(PLCData.getInstance().getHoldingRegister(1, 0x162) & 0xff);
                builder.append("\r\n");

                builder.append("小车 PLC Version:\t\t\t");
                builder.append(SmaroWindingMachineCanReadSucceedListener.getInstance().dofferVersion);
            }

        } else {
            builder.append("机头 PLC Version:\t\t\t");
            builder.append(FxPLCData.getInstance().getValue(2, 113));
            builder.append(FxPLCData.getInstance().getValue(2, 114));
//            builder.append("\r\n");
//            builder.append("CBF PLC Version:\t\t\t\t");
//            builder.append(SmaroWindingMachineActivity.fxplc.getValue(4,321));
//            builder.append(SmaroWindingMachineActivity.fxplc.getValue(4,322));
//            builder.append("\r\n");
//            builder.append("纱库 PLC Version:\t\t\t");
//            builder.append(SmaroWindingMachineActivity.fxplc.getValue(5,323));
//            builder.append(SmaroWindingMachineActivity.fxplc.getValue(5,324));
//            builder.append("\r\n");
//            builder.append("小车 PLC Version:\t\t\t");
//            builder.append(SmaroWindingMachineActivity.fxplc.getValue(6,176));
//            builder.append(SmaroWindingMachineActivity.fxplc.getValue(6,177));
        }

        ((TextView) getActivity().findViewById(R.id.PLC_version)).setText(builder.toString());
        qrcode = getActivity().findViewById(R.id.qrcode);
        StringBuilder qrContent = new StringBuilder();
        qrContent.append("http://101.200.182.225:5010/OfflineImport/AndroidHmi?");
        qrContent.append("uid=").append(MD5.readLine("//data//serial")).append("&");
        qrContent.append("softwareVersion=").append(AppContext.version).append("&");
        String hardware = Build.VERSION.RELEASE.equals("6.0.1") ? "15inch" : "10.2inch";
        qrContent.append("hardwareInfo=").append(hardware);
        Bitmap bitmap = QRCodeUtil.createQRCodeBitmap(qrContent.toString(), 240, 240);
        qrcode.setImageBitmap(bitmap);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = ViewCache.getInstance().getView(R.layout.fragment_equipment_maintenance_software_upgrade);

        return v;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.exit_btn:
                ObjectBox.get().close();
                android.os.Process.killProcess(android.os.Process.myPid());
                break;

            default:
                break;
        }
    }

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        return true;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            StringBuilder builder = new StringBuilder();
            if (MachineParameterSharedPreference.getInstance().getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
                String language = Locale.getDefault().getLanguage();
                if (language.equals("en")) {
                    builder.append("Main PLC Version:\t\t\t");
                    builder.append("L");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0xF9));
                    builder.append("J.");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0xFA));
                    builder.append("P.V");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0xFB) >> 8);
                    builder.append(".");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0xFB) & 0xff);
                    builder.append("\r\n");

                    builder.append("CBF PLC Version:\t\t\t\t");
                    builder.append("L");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x131));
                    builder.append("C.");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x132));
                    builder.append("P.V");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x13E) >> 8);
                    builder.append(".");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x13E) & 0xff);
                    builder.append("\r\n");

                    builder.append("Magazine PLC Version:\t\t\t");
                    builder.append("L");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x15E));
                    builder.append("S.");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x15F));
                    builder.append("P.V");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x162) >> 8);
                    builder.append(".");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x162) & 0xff);
                    builder.append("\r\n");

                    builder.append("AD PLC Version:\t\t\t");
                    builder.append(SmaroWindingMachineCanReadSucceedListener.getInstance().dofferVersion);
                } else {
                    builder.append("机头 PLC Version:\t\t\t");
                    builder.append("L");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0xF9));
                    builder.append("J.");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0xFA));
                    builder.append("P.V");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0xFB) >> 8);
                    builder.append(".");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0xFB) & 0xff);
                    builder.append("\r\n");

                    builder.append("CBF PLC Version:\t\t\t\t");
                    builder.append("L");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x131));
                    builder.append("C.");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x132));
                    builder.append("P.V");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x13E) >> 8);
                    builder.append(".");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x13E) & 0xff);
                    builder.append("\r\n");

                    builder.append("纱库 PLC Version:\t\t\t");
                    builder.append("L");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x15E));
                    builder.append("S.");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x15F));
                    builder.append("P.V");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x162) >> 8);
                    builder.append(".");
                    builder.append(PLCData.getInstance().getHoldingRegister(1, 0x162) & 0xff);
                    builder.append("\r\n");

                    builder.append("小车 PLC Version:\t\t\t");
                    builder.append(SmaroWindingMachineCanReadSucceedListener.getInstance().dofferVersion);
                }

            } else {
                builder.append("机头 PLC Version:\t\t\t");
                builder.append(FxPLCData.getInstance().getValue(2, 113));
                builder.append(FxPLCData.getInstance().getValue(2, 114));
//            builder.append("\r\n");
//            builder.append("CBF PLC Version:\t\t\t\t");
//            builder.append(SmaroWindingMachineActivity.fxplc.getValue(4,321));
//            builder.append(SmaroWindingMachineActivity.fxplc.getValue(4,322));
//            builder.append("\r\n");
//            builder.append("纱库 PLC Version:\t\t\t");
//            builder.append(SmaroWindingMachineActivity.fxplc.getValue(5,323));
//            builder.append(SmaroWindingMachineActivity.fxplc.getValue(5,324));
//            builder.append("\r\n");
//            builder.append("小车 PLC Version:\t\t\t");
//            builder.append(SmaroWindingMachineActivity.fxplc.getValue(6,176));
//            builder.append(SmaroWindingMachineActivity.fxplc.getValue(6,177));
            }
        }
    }
}
